/*
 * ContribuyentesBusquedaFrame.java
 *
 * Created on 16 de abril de 2008, 10:08 AM
 */
package org.pagos.comercios;


import org.mybeans.Sesion;
import org.mybeans.ContribuyentesBeans;
 
import org.mybeans.ComerciosBeans;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author  avbravo
 */
public class ComerciosArregloPagoListadoFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    ComerciosBeans comerciosBeans;

    /** Creates new form ContribuyentesBusquedaFrame */
    public ComerciosArregloPagoListadoFrame() {
        initComponents();
        sesion = new Sesion();
        /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */
        comerciosBeans = new ComerciosBeans();
        jXTable1.setVisible(false);
        String sql = "select idcomercio, comercio,  idarreglopago, fechaarreglopago,morosidadinicial,mesesmorosos,saldoactual " +
                " from comercios, arreglopago " +
                " where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='si' " +
                " and arreglopago.idmunicipio ='" + sesion.getIdmunicipio() + "' and arreglopago.id = comercios.idcomercio " +
                " and arreglopago.tipoid ='comercios' and arreglopago.pagado='no' and arreglopago.activo='si' " +
                " order by comercio";
        CargarTodosRegistros(sql);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Listado de Comercios con Arreglo de Pago");

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "No.", "Comercio", "No.", "Fecha", "Saldo", "Meses", "Deuda"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jXTable1.setOpaque(false);
        jScrollPane1.setViewportView(jXTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosArregloPagoListadoFrame().setVisible(true);
            }
        });
    }

    public void CargarTodosRegistros(String sql) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(null, "No existen registros con esa condicion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

 modelo = (DefaultTableModel) jXTable1.getModel();

       // modelo.isCellEditable(1, 1);
        TableColumn columnCedula = jXTable1.getColumnModel().getColumn(0);
        columnCedula.setPreferredWidth(90);

        TableColumn columnContribuyente = jXTable1.getColumnModel().getColumn(1);
        columnContribuyente.setPreferredWidth(200);

        TableColumn columnId = jXTable1.getColumnModel().getColumn(2);
        columnId.setPreferredWidth(60);

TableColumn columnFecha = jXTable1.getColumnModel().getColumn(3);
        columnFecha.setPreferredWidth(80);

// Bucle para cada resultado en la consulta
            rs.previous();
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[7]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idcomercio"); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

                fila[1] = rs.getString("comercio");
            //    fila[2] = rs.getString("domicilio");
                fila[2] = rs.getString("idarreglopago");
                //java.util.Date myFechaDatos = rs.getDate("fechaarreglopago");
                Format formatter;
                formatter = new SimpleDateFormat("dd/MM/yyyy");
                Date fecha = rs.getDate("fechaarreglopago");
                String MyFecha = formatter.format(fecha);

                fila[3] = MyFecha;
            fila[4] = rs.getString("morosidadinicial");
            fila[5] = rs.getString("mesesmorosos");
            fila[6] = rs.getString("saldoactual");
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);
            //aris++
            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                     
                       
                    }
                }
            });

        } catch (Exception ex) {
          sesion.MostrarMensajeError("Error ", "Metodo:CargarTodosRegistros()\n " + ex.getMessage().toString());
        }
    }
    
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
